/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.actor;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.cxhd.pacs.DataConfig;
import com.cxhd.pacs.StaticData;
import com.cxhd.pacs.bean.UserBean;
import com.cxhd.pacs.external.IntefaceOther;
import com.cxhd.pacs.tools.DicomImage;
import com.cxhd.pacs.tools.HttpClient;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author Administrator
 */
public class SendSmsActor extends UntypedActor {

    @Override
    public void onReceive(Object o) throws Exception {
        Map<String, String> map = (Map) o;

        if (map.get("msgType")!=null&&map.get("msgType").equalsIgnoreCase("sendDoctorAndsender")) {
            sendDoctorAndSender(map);
        } else if (map.get("msgType")!=null&&map.get("msgType").equalsIgnoreCase("queryRpt")) {
            sendSmsForYzm(map);
        } else {
            getPhone(map);
        }
    }

    public void getPhone(Map<String, String> map) {
        String content = "(" + map.get("hospital") + ") 病人名字：" + map.get("name") + " ";
        UserBean ub = new Gson().fromJson(IntefaceOther.getDoctorByGroup(StaticData.DOCTOR_MANAGER_GROUP), new TypeToken<UserBean>() {
        }.getType());
        content += " " + DataConfig.getInstance().getConfig().get("localUrl") + "/MedicalInfo/other/openApply?p=" + map.get("ask_pk") + "&u=";
        List<Map<String, String>> data = ub.getData();
        for (Map<String, String> user : data) {
            sendSms(user.get("phone"), content + user.get("username"));
        }
    }

    public void sendDoctorAndSender(Map<String, String> map) {
        String content = "(" + map.get("hospital_name") + ") 病人名字：" + map.get("patient_name") + " 性别：" + map.get("sex") + " 年龄：" + map.get("age") + " 预约时间：" + map.get("appointment_time");
        sendSms(map.get("recvicer_phone"), content);
        content = "  您发送的远程网络门诊已经受理 病人名字：" + map.get("patient_name") + " 性别：" + map.get("sex") + " 年龄：" + map.get("age") + " 预约时间：" + map.get("appointment_time") + " 预约医生：" + map.get("recvicer_name");
        sendSms(map.get("sender_phone"), content);
    }

    public void sendSms(String to, String content) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("phone", to));
        params.add(new BasicNameValuePair("content", content));
        System.out.println("phone:" + to + "_______content:" + content);
        HttpClient.httpPost("http://" + DataConfig.getInstance().getConfig().get("smsurl") + "/SmsServer/smsForwangluomenzhen.jsp", params);

    }

    public void sendSmsForYzm(Map<String, String> map) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("phone", map.get("to")));
        params.add(new BasicNameValuePair("content", map.get("yzm")));
        System.out.println("phone:" + map.get("to") + "_______content:" + map.get("yzm"));
        HttpClient.httpPost("http://" + DataConfig.getInstance().getConfig().get("smsurl") + "/SmsServer/smsForRptQuery.jsp", params);

    }

}
